/*
 * Copyright 2014 Semidata. All rights reserved.
 */
package com.semidata.trp.report.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.report.data.ReportData;
import com.semidata.trp.report.data.ReportEntity;

/**
 * @author shewei@semidata.com, She Wei
 * 
 */
public class DatabaseUtils {

	public static void writeMonthlyReport(ReportData reportData, String month) {
		write(month, 3, reportData);
	}

	public static void writeWeeklyReport(ReportData reportData, String startDay) {
		Calendar cal = Calendar.getInstance();
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		try {
			cal.setTime(format.parse(startDay));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

		cal.add(Calendar.DAY_OF_MONTH, 6);

		write(format.format(cal.getTime()), 2, reportData);
	}

	public static void writeDailyReport(ReportData reportData, String day) {
		write(day, 1, reportData);
	}

	public static String getFirstDayOf7Days(String date) {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		Calendar c = new GregorianCalendar();
		c.set(Integer.valueOf(date.substring(0, 4)),
		        Integer.valueOf(date.substring(4, 6)) - 1,
		        Integer.valueOf(date.substring(6)) - 6);
		String firstDayOf7Days = dateformat.format(c.getTime());
		dateformat = null;
		c = null;
		return firstDayOf7Days;
	}

	public static void clearSql(String date, int dateType) {
		List<String> sqls = new ArrayList<String>();
		StringBuffer sql = new StringBuffer();
		String sqlHeader = "delete from ";
		String tableHeader = "";
		String dateTag = "";
		switch (dateType) {
		case 1:
			tableHeader = "trp_daily";
			dateTag = "date";
			break;
		case 2:
			tableHeader = "trp_weekly";
			dateTag = "end";
			break;
		case 3:
			tableHeader = "trp_monthly";
			dateTag = "month";
			break;
		}
		sqlHeader = sqlHeader.concat(tableHeader);
		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("report");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("mediareport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("mediatypereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationmediareport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationmediatypereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationpositionreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("organizationreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("positionreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategypositionreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategymediatypereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategyreport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("resourcereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());

		sql.setLength(0);
		sql.append(sqlHeader);
		sql.append("strategyresourcereport");
		sql.append(" where ");
		sql.append(dateTag);
		sql.append("=\"");
		sql.append(date);
		sql.append("\";");
		sqls.add(sql.toString());
		/*
		 * sql.setLength(0); sql.append(sqlHeader);
		 * sql.append("organizationresourcereport"); sql.append(" where ");
		 * sql.append(dateTag); sql.append("=\""); sql.append(date);
		 * sql.append("\";"); sqls[13] = sql.toString();
		 */
		for (int i = 0; i < sqls.size(); i++) {
			Logger.log("DEBUG", sqls.get(i));
		}

		// SessionUtils.executeSql(sqls);
		SqlUtils.executeSql((String[]) sqls.toArray(new String[sqls.size()]));
	}

	private static void write(String date, int dateType, ReportData reportData) {

		int valuesCount = 0;
		List<String> sqls = new ArrayList<>();
		String sqlHeader = "insert into ";
		String tableHeader = "";
		StringBuffer sql = new StringBuffer();
		String dateTag = "";
		String firstDay = "";
		switch (dateType) {
		case 1:
			tableHeader = "trp_daily";
			dateTag = "date";
			break;
		case 2:
			tableHeader = "trp_weekly";
			dateTag = "start,end";
			firstDay = getFirstDayOf7Days(date);
			break;
		case 3:
			tableHeader = "trp_monthly";
			dateTag = "month";
			break;
		}
		sqlHeader = sqlHeader.concat(tableHeader);
		ReportEntity entity = reportData.getTotal();

		if (entity.getPv() > 0 || entity.getClick() > 0) {
			sql.setLength(0);
			sql.append(sqlHeader);
			sql.append("report");
			sql.append("(pv,uv,click,cost,");
			sql.append(dateTag);
			sql.append(") values (");
			sql.append(entity.getPv());
			sql.append(",");
			sql.append(entity.getUv());
			sql.append(",");
			sql.append(entity.getClick());
			sql.append(",");
			sql.append(entity.getCost());
			sql.append(",\"");
			if (dateType == 2) {
				sql.append(firstDay);
				sql.append("\",\"");
			}
			sql.append(date);
			sql.append("\");");
			sqls.add(sql.toString());

			if (reportData.getMedia() == null
			        || reportData.getMedia().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, ReportEntity> entryReportData : reportData
				        .getMedia().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("mediareport");
						sql.append("(media_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					entity = entryReportData.getValue();
					sql.append(entity.getPv());
					sql.append(",");
					sql.append(entity.getUv());
					sql.append(",");
					sql.append(entity.getClick());
					sql.append(",");
					sql.append(entity.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getType() == null || reportData.getType().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				sql.append(sqlHeader);
				sql.append("mediatypereport");
				sql.append("(type,pv,uv,click,cost,");
				sql.append(dateTag);
				sql.append(") values ");
				for (int t = 0; t < 3; t++) {
					sql.append("(");
					sql.append(t);
					sql.append(",");
					if (reportData.getType().get(t) != null) {
						entity = reportData.getType().get(t);
						sql.append(entity.getPv());
						sql.append(",");
						sql.append(entity.getUv());
						sql.append(",");
						sql.append(entity.getClick());
						sql.append(",");
						sql.append(entity.getCost());
					} else {
						sql.append("0,0,0,0");
					}
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getPosition() == null
			        || reportData.getPosition().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, ReportEntity> entryReportData : reportData
				        .getPosition().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("positionreport");
						sql.append("(position_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					entity = entryReportData.getValue();
					sql.append(entity.getPv());
					sql.append(",");
					sql.append(entity.getUv());
					sql.append(",");
					sql.append(entity.getClick());
					sql.append(",");
					sql.append(entity.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrg() == null || reportData.getOrg().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, ReportEntity> entryReportData : reportData
				        .getOrg().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("organizationreport");
						sql.append("(organization_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					entity = entryReportData.getValue();
					sql.append(entity.getPv());
					sql.append(",");
					sql.append(entity.getUv());
					sql.append(",");
					sql.append(entity.getClick());
					sql.append(",");
					sql.append(entity.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgMedia() == null
			        || reportData.getOrgMedia().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, ReportEntity>> entryOrgMediaReport : reportData
				        .getOrgMedia().entrySet()) {
					int fKey = entryOrgMediaReport.getKey();
					for (Map.Entry<Integer, ReportEntity> entryMediaReport : reportData
					        .getOrgMedia().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("organizationmediareport");
							sql.append("(organization_id,media_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryMediaReport.getKey());
						sql.append(",");
						entity = entryMediaReport.getValue();
						sql.append(entity.getPv());
						sql.append(",");
						sql.append(entity.getUv());
						sql.append(",");
						sql.append(entity.getClick());
						sql.append(",");
						sql.append(entity.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgType() == null
			        || reportData.getOrgType().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, ReportEntity>> entryOrgMediaReport : reportData
				        .getOrgType().entrySet()) {
					int fKey = entryOrgMediaReport.getKey();
					for (int t = 0; t < 3; t++) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("organizationmediatypereport");
							sql.append("(organization_id,type,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(t);
						sql.append(",");
						if (entryOrgMediaReport.getValue().get(t) != null) {
							entity = entryOrgMediaReport.getValue().get(t);
							sql.append(entity.getPv());
							sql.append(",");
							sql.append(entity.getUv());
							sql.append(",");
							sql.append(entity.getClick());
							sql.append(",");
							sql.append(entity.getCost());
						} else {
							sql.append("0,0,0,0");
						}
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getOrgPosition() == null
			        || reportData.getOrgPosition().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, ReportEntity>> entryOrgPosReport : reportData
				        .getOrgPosition().entrySet()) {
					int fKey = entryOrgPosReport.getKey();
					for (Map.Entry<Integer, ReportEntity> entryPosReport : reportData
					        .getOrgPosition().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("organizationpositionreport");
							sql.append("(organization_id,position_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryPosReport.getKey());
						sql.append(",");
						entity = entryPosReport.getValue();
						sql.append(entity.getPv());
						sql.append(",");
						sql.append(entity.getUv());
						sql.append(",");
						sql.append(entity.getClick());
						sql.append(",");
						sql.append(entity.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStrategy() == null
			        || reportData.getStrategy().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Entry<Integer, ReportEntity> entryStraReport : reportData
				        .getStrategy().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("strategyreport");
						sql.append("(strategy_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryStraReport.getKey());
					sql.append(",");
					entity = entryStraReport.getValue();
					sql.append(entity.getPv());
					sql.append(",");
					sql.append(entity.getUv());
					sql.append(",");
					sql.append(entity.getClick());
					sql.append(",");
					sql.append(entity.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStrategyPosition() == null
			        || reportData.getStrategyPosition().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, ReportEntity>> entryStraPosReport : reportData
				        .getStrategyPosition().entrySet()) {
					int fKey = entryStraPosReport.getKey();
					for (Map.Entry<Integer, ReportEntity> entryPosReport : reportData
					        .getStrategyPosition().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategypositionreport");
							sql.append("(strategy_id,position_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryPosReport.getKey());
						sql.append(",");
						entity = entryPosReport.getValue();
						sql.append(entity.getPv());
						sql.append(",");
						sql.append(entity.getUv());
						sql.append(",");
						sql.append(entity.getClick());
						sql.append(",");
						sql.append(entity.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStrategyType() == null
			        || reportData.getStrategyType().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, ReportEntity>> entryStraMediaTypeReport : reportData
				        .getStrategyType().entrySet()) {
					int fKey = entryStraMediaTypeReport.getKey();
					for (int t = 0; t < 3; t++) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategymediatypereport");
							sql.append("(strategy_id,type,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(t);
						sql.append(",");
						if (entryStraMediaTypeReport.getValue().get(t) != null) {
							entity = entryStraMediaTypeReport.getValue().get(t);
							sql.append(entity.getPv());
							sql.append(",");
							sql.append(entity.getUv());
							sql.append(",");
							sql.append(entity.getClick());
							sql.append(",");
							sql.append(entity.getCost());
						} else {
							sql.append("0,0,0,0");
						}
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getResource() == null
			        || reportData.getResource().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, ReportEntity> entryReportData : reportData
				        .getResource().entrySet()) {
					if (valuesCount % 1000 == 0) {
						if (sql.length() > 0) {
							sql.delete(sql.length() - 1, sql.length());
							sqls.add(sql.toString());
						}
						sql.setLength(0);
						sql.append(sqlHeader);
						sql.append("resourcereport");
						sql.append("(resource_id,pv,uv,click,cost,");
						sql.append(dateTag);
						sql.append(") values ");
					}
					sql.append("(");
					sql.append(entryReportData.getKey());
					sql.append(",");
					entity = entryReportData.getValue();
					sql.append(entity.getPv());
					sql.append(",");
					sql.append(entity.getUv());
					sql.append(",");
					sql.append(entity.getClick());
					sql.append(",");
					sql.append(entity.getCost());
					sql.append(",\"");
					if (dateType == 2) {
						sql.append(firstDay);
						sql.append("\",\"");
					}
					sql.append(date);
					sql.append("\"),");
					valuesCount = valuesCount + 1;
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}

			if (reportData.getStrategyResource() == null
			        || reportData.getStrategyResource().isEmpty()) {
				;
			} else {
				valuesCount = 0;
				sql.setLength(0);
				for (Map.Entry<Integer, Map<Integer, ReportEntity>> entryOrgMediaReport : reportData
				        .getStrategyResource().entrySet()) {
					int fKey = entryOrgMediaReport.getKey();
					for (Map.Entry<Integer, ReportEntity> entryResReport : reportData
					        .getStrategyResource().get(fKey).entrySet()) {
						if (valuesCount % 1000 == 0) {
							if (sql.length() > 0) {
								sql.delete(sql.length() - 1, sql.length());
								sqls.add(sql.toString());
							}
							sql.setLength(0);
							sql.append(sqlHeader);
							sql.append("strategyresourcereport");
							sql.append("(strategy_id,resource_id,pv,uv,click,cost,");
							sql.append(dateTag);
							sql.append(") values ");
						}
						sql.append("(");
						sql.append(fKey);
						sql.append(",");
						sql.append(entryResReport.getKey());
						sql.append(",");
						entity = entryResReport.getValue();
						sql.append(entity.getPv());
						sql.append(",");
						sql.append(entity.getUv());
						sql.append(",");
						sql.append(entity.getClick());
						sql.append(",");
						sql.append(entity.getCost());
						sql.append(",\"");
						if (dateType == 2) {
							sql.append(firstDay);
							sql.append("\",\"");
						}
						sql.append(date);
						sql.append("\"),");
						valuesCount = valuesCount + 1;
					}
				}
				sql.delete(sql.length() - 1, sql.length());
				sqls.add(sql.toString());
			}
		}

		for (int i = 0; i < sqls.size(); i++) {
			Logger.log("DEBUG", sqls.get(i));
		}

		clearSql(date, dateType);

		SqlUtils.executeSql((String[]) sqls.toArray(new String[sqls.size()]));
	}

}
